//
//  _CXHWebImageSetter.h
//  CXHAdSDK-Kit
//
//  Created by 陈坤 on 2022/10/24.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <pthread.h>
#import "CXHAdImageLoader.h"

NS_ASSUME_NONNULL_BEGIN

/**
 Submits a block for execution on a main queue and waits until the block completes.
 */
static inline void _cxh_dispatch_sync_on_main_queue(void (^block)()) {
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
}

extern NSString *const _CXHWebImageFadeAnimationKey;
extern const NSTimeInterval _CXHWebImageFadeTime;
extern const NSTimeInterval _CXHWebImageProgressiveFadeTime;

@interface _CXHWebImageSetter : NSObject

/// Current image url.
@property (nullable, nonatomic, readonly) NSURL *imageURL;
/// Current sentinel.
@property (nonatomic, readonly) int32_t sentinel;

/// Create new operation for web image and return a sentinel value.
- (int32_t)setOperationWithSentinel:(int32_t)sentinel
                                url:(nullable NSURL *)imageURL
                            options:(CXHWebImageOptions)options
                            manager:(CXHAdImageLoader *)manager
                           progress:(nullable CXHWebImageProgressBlock)progress
                          transform:(nullable CXHWebImageTransformBlock)transform
                         completion:(nullable CXHWebImageCompletionBlock)completion;

/// Cancel and return a sentinel value. The imageURL will be set to nil.
- (int32_t)cancel;

/// Cancel and return a sentinel value. The imageURL will be set to new value.
- (int32_t)cancelWithNewURL:(nullable NSURL *)imageURL;

/// A queue to set operation.
+ (dispatch_queue_t)setterQueue;


@end

NS_ASSUME_NONNULL_END
